package com.hll.api.controller.kaola;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.hll.api.config.web.RespMessage;
import com.hll.api.entity.ShopCar;
import com.hll.api.entity.User;
import com.hll.api.service.IShopCarService;
import com.hll.api.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 购物车 前端控制器
 * </p>
 *
 * @author 何龙
 * @since 2019-06-29
 */
@RestController
@RequestMapping("/api/kaola/shop/car")
public class ShopCarController {
    @Resource
    IShopCarService service;
    @Resource
    UserService userService;

    /**
     * @api {post} /api/kaola/shop/car/add/{id} 增加购物车
     * @apiVersion 1.0.0
     * @apiName carAdd
     * @apiGroup kaola
     * @apiParam {int} id 商品id
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/shop/car/add/10000
     */
    @PostMapping("/add/{id}")
    public RespMessage add(@PathVariable String id, HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<ShopCar> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).eq( "goods_id", id );
        ShopCar one = service.getOne( wrapper );
        if (one != null) {
            one.setNum( one.getNum() + 1 );
            service.updateById( one );
            return RespMessage.success( "收藏成功" );
        }
        ShopCar shopCar = new ShopCar();
        shopCar.setDate( new Date() );
        shopCar.setGoodsId( id );
        shopCar.setNum( 1 );
        shopCar.setUserId( user.getId().intValue() );
        service.save( shopCar );
        return RespMessage.success( "收藏成功" );
    }

    /**
     * @api {delete} /api/kaola/shop/car/del/{id} 删除购物车
     * @apiVersion 1.0.0
     * @apiName carDel
     * @apiGroup kaola
     * @apiParam {int} id 商品id
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/shop/car/del/10000
     */
    @DeleteMapping("del/{id}")
    public RespMessage del(@PathVariable Integer id, HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<ShopCar> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).eq( "goods_id", id );
        service.remove( wrapper );
        return RespMessage.success( "删除成功" );
    }

    /**
     * @api {get} /api/kaola/shop/car/get 购物车列表
     * @apiVersion 1.0.0
     * @apiName carGet
     * @apiGroup kaola
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/shop/car/get
     */
    @GetMapping("/get")
    public RespMessage get(HttpServletRequest request) {
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<ShopCar> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).orderByDesc( "date" );
        List<ShopCar> list = service.list( wrapper );
        return RespMessage.success( list, "" );
    }

    /**
     * @api {put} /api/kaola/shop/car/update/{id}/{num} 修改购物车商品数量
     * @apiVersion 1.0.0
     * @apiName Carupdate
     * @apiGroup kaola
     * @apiParam {int} id 商品id
     * @apiParam {int} num 商品数量
     * @apiHeader {String} userToken 用户授权登录token
     * @apiParamExample {string} 请求示例:
     * /api/kaola/shop/car/update/1000/2
     */
    @PutMapping("/update/{id}/{num}")
    public RespMessage update(@PathVariable Integer id, @PathVariable Integer num, HttpServletRequest request) {
        if (num < 0) {
            return RespMessage.error( "商品数量不能为负数" );
        }
        User user = userService.getUser( request );
        if (user == null) {
            return RespMessage.loginError();
        }
        QueryWrapper<ShopCar> wrapper = new QueryWrapper<>();
        wrapper.eq( "user_id", user.getId() ).eq( "goods_id", id );
        ShopCar shopCar = service.getOne( wrapper );
        if (shopCar == null) {
            return RespMessage.error( id + "号商品不存在" );
        }
        shopCar.setNum( num );
        service.updateById( shopCar );
        return RespMessage.success( "修改成功" );
    }

}
